/**
 * Created by 白雨浓 on 2017/12/23 12:14.
 */
export default {
  select(self){
    let output = `SELECT * FROM ${self.tableName}
WHERE state != 0`;

    for (const param of self.params) {
      if (param.type === 'int' || param.type === 'boolean') continue;
      if (param.name === 'state') continue;
      if (param.name.toLowerCase().indexOf('time') >= 0) continue;
      output += `\n<if test="${param.name} != null">AND ${self.toUnderline(param.name)} = #{${param.name}}</if>`
    }
    self.output = output;
  },


  selectPage(self){
    let output = `SELECT * FROM ${self.tableName}
WHERE state != 0`;

    for (const param of self.params) {
      if (param.type === 'int' || param.type === 'boolean') continue;
      if (param.name === 'state') continue;
      if (param.name.toLowerCase().indexOf('time') >= 0) continue;
      output += `\n<if test="${param.name} != null">AND ${self.toUnderline(param.name)} = #{${param.name}}</if>`
    }

    output += '\nORDER BY id\nLIMIT #{start}, #{size}';
    output += '\n\n\n' + this.selectPageTotal(self);
    self.output = output;
  },


  selectPageTotal(self){
    let output = `SELECT COUNT(${self.tableName}.id)
FROM ${self.tableName}
WHERE state != 0`;
    for (const param of self.params) {
      if (param.type === 'int' || param.type === 'boolean') continue;
      if (param.name === 'state') continue;
      if (param.name.toLowerCase().indexOf('time') >= 0) continue;
      output += `\n<if test="${param.name} != null">AND ${self.toUnderline(param.name)} = #{${param.name}}</if>`
    }
    return output;
  },


  selectSearch(self, keyword){
    let output = `SELECT *
FROM ${self.tableName}
WHERE state != 0`;
    output += `\n AND ${self.tableName}.${self.toUnderline(keyword)} LIKE CONCAT('%', #{${keyword}}, '%')`;
    output += '\n\n\n' + this.selectSearchTotal(self, keyword);
    self.output = output;
  },


  selectSearchTotal(self, keyword){
    let output = `SELECT COUNT(${self.tableName}.id)
FROM ${self.tableName}
WHERE state != 0`;
    output += `\n AND ${self.tableName}.${self.toUnderline(keyword)} LIKE CONCAT('%', #{${keyword}}, '%')`;
    return output;
  },

}
